

# Create your views here.
# from django.contrib.auth.models import User
import json
import re

from django.contrib.auth import get_user_model, login
from django_redis import get_redis_connection

User = get_user_model()
from django.views import View
from django.http import JsonResponse
from django.shortcuts import render



'''
判断用户名是否重复的功能
前段： 用户输入用户名，失去焦点后发送 axios（ajax）
后端：
    请求 ：接受用户名
    业务逻辑 ：根据用户名查询数据库，如果数量等于0中午没有注册
    响应：  JSON   {code：0，count：1，errmsg:ok}
    路由 ： GET 
'''

class   UsernameCountView(View):

    def  get(self,request,username):
        # 接收用户名，根据用户名去查询数据库
        # if not re.match('[a-zA-Z0-9_-]{5,20}',username):
        #     return JsonResponse({'code':200,'errmsg':'用户名不满足条件'})
        count = User.objects.filter(username=username).count()
        return JsonResponse({'code':0,'count':count,'errmsg':'ok'})

class   MobilecountView(View):

    def  get(self,request,mobile):
        # 接收用户名，根据用户名去查询数据库
        # if not re.match('[a-zA-Z0-9_-]{5,20}',username):
        #     return JsonResponse({'code':200,'errmsg':'用户名不满足条件'})
        count = User.objects.filter(mobile=mobile).count()
        return JsonResponse({'code':0,'count':count,'errmsg':'ok'})

class RegisterView(View):

    def post(self,requset):
         boby_bytes = requset.body
         boby_str = boby_bytes.decode()
         boby_dict = json.loads(boby_str)

         username = boby_dict.get('username')
         password = boby_dict.get('password')
         password2 = boby_dict.get('password2')
         mobile = boby_dict.get('mobile')
         sms_code = boby_dict.get('sms_code')
         allow = boby_dict.get('allow')

         if not  all([username,password,password2,mobile,allow]):
             return JsonResponse({'code':'400','errmsg':'参数不全'})
         if not re.match('[a-zA-Z0-9_-]{5,20}',username):
             return  JsonResponse({'code':'400','errmsg':'用户名称不满足规则'})
         if not re.match('[a-zA-Z0-9_-]{8,20}',password):
             return  JsonResponse({'code':'400','errmsg':'密码不满足规则'})
         if password != password2:
             return  JsonResponse({'code':'400','errmsg':'二次输入的密码不对'})
         if not re.match('1[345789]\d{9}',mobile):
             return  JsonResponse({'code':'400','errmsg':'手机号不满足规则'})
         if allow is False:
             return JsonResponse({'code': '400', 'errmsg': '没有勾选'})
         redis_cli = get_redis_connection('code')
         redis_sms_code = redis_cli.get(mobile)
         if sms_code!= redis_sms_code.decode():
             return JsonResponse({'code': '400', 'errmsg': '验证码不正确'})
         # save、create密码都没有加密
         # user = User(username=username,password=password,mobile=mobile)
         # user.save()
         # User.objects.create(username=username, password=password, mobile=mobile)
         # 保存数据
         user = User.objects.create_user(username=username, password=password, mobile=mobile)
         # session
         # requset.session['udser_id']=user.id

         login(requset,user)

         return JsonResponse({'code':0,'errmsg':'ok'})




